/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import app.Global;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author yoshi
 */
public class EmployeeSchedule {

    private long id;
    private Date date;
    private short status;
    private long employeeId;
    private long timeSlotId;
    private Employee employee;
    private TimeSlot timeSlot;
    private EmployeeShiftRequest request;

    public EmployeeSchedule(ResultSet rs) {
        this.loadFromResultSet(rs);
    }
    
    public EmployeeSchedule(long id, Date date, Employee employee, TimeSlot timeSlot) {
        this.id = id;
        this.date = date;
        this.employee = employee;
        this.employeeId = employee.getId();
        this.timeSlot = timeSlot;
        this.timeSlotId = timeSlot.getId();
    }

    public EmployeeShiftRequest getRequest() {
        if (request != null) {
            return request;
        } else {
            Connection connection = Global.getConnection();
            PreparedStatement st = null;
            ResultSet rs = null;

            try {
                st = connection.prepareStatement("SELECT TOP 1 * FROM employee_shift_requests WHERE from_schedule_id = ? AND status >= 1");
                st.setLong(1, this.getId());
                rs = st.executeQuery();
                if (rs.next()) {
                    request = new EmployeeShiftRequest(rs.getLong("id"), rs.getLong("from_schedule_id"), rs.getLong("employee_id"), rs.getDate("request_date"), rs.getLong("time_slot_id"), rs.getString("note"), rs.getShort("status"));
                }

            } catch (SQLException ex) {
                Logger.getLogger(EmployeeSchedule.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    rs.close();
                    st.close();
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(EmployeeSchedule.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    return request;
                }
            }
        }
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
        this.employeeId = employee.getId();
    }

    public long getId() {
        return id;
    }

    private void setId(long id) {
        this.id = id;
    }

    public short getStatus() {
        return status;
    }

    public void setStatus(short status) {
        this.status = status;
    }

    public TimeSlot getTimeSlot() {
        return timeSlot;
    }

    public void setTimeSlot(TimeSlot timeSlot) {
        this.timeSlot = timeSlot;
        this.timeSlotId = timeSlot.getId();
    }

    public long getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(long employeeId) {
        if (this.employee != null) {
            return;
        }
        this.employeeId = employeeId;
    }

    public long getTimeSlotId() {
        return timeSlotId;
    }

    public void setTimeSlotId(long timeSlotId) {
        if (this.timeSlot != null) {
            return;
        }
        this.timeSlotId = timeSlotId;
    }

    private void loadFromResultSet(ResultSet rs) {
        try {
            Employee e = null;
            TimeSlot ts = null;
            EmployeeSchedule schedule = null;
            Roll roll = null;

            roll = new Roll(rs.getLong("roll_id"), rs.getString("roll_name"), rs.getBoolean("roll_head"));
            e = new Employee(rs.getLong("employee_id"), rs.getString("username"), rs.getString("first_name"), rs.getString("last_name"), rs.getBoolean("gender"), roll);
            ts = new TimeSlot(rs.getLong("time_slot_id"), rs.getTime("from"), rs.getTime("to"));
            
            this.setId(rs.getLong("id"));
            this.setDate(rs.getDate("date"));
            this.setEmployee(e);
            this.setTimeSlot(ts);
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeSchedule.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final EmployeeSchedule other = (EmployeeSchedule) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + (int) (this.id ^ (this.id >>> 32));
        hash = 67 * hash + (this.date != null ? this.date.hashCode() : 0);
        hash = 67 * hash + this.status;
        hash = 67 * hash + (int) (this.employeeId ^ (this.employeeId >>> 32));
        hash = 67 * hash + (int) (this.timeSlotId ^ (this.timeSlotId >>> 32));
        hash = 67 * hash + (this.employee != null ? this.employee.hashCode() : 0);
        hash = 67 * hash + (this.timeSlot != null ? this.timeSlot.hashCode() : 0);
        hash = 67 * hash + (this.request != null ? this.request.hashCode() : 0);
        return hash;
    }
}
